package com.ywd.platform.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.ywd.platform.constants.JwtConstant;
import com.ywd.platform.constants.RedisPrefixConstants;
import com.ywd.platform.dto.UserDto;
import com.ywd.platform.dto.UserLoginDto;
import com.ywd.platform.page.UserPageDto;
import com.ywd.platform.entity.UserEntity;
import com.ywd.platform.exception.CommonException;
import com.ywd.platform.exception.EmError;
import com.ywd.platform.exception.RPage;
import com.ywd.platform.mapper.UserMapper;
import com.ywd.platform.properties.DefaultProperties;
import com.ywd.platform.utils.JwtTokenUtil;
import com.ywd.platform.utils.RedisUtil;
import com.ywd.platform.vo.UserVo;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * YWD
 * 2025/8/15
 */
@AllArgsConstructor
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper,UserEntity> {

    private final RedisUtil redisUtil;
    private final DefaultProperties defaultProperties;

    /**
     * 登录
     */
    public UserVo login(UserLoginDto dto) {
        if (StringUtils.isBlank(dto.getAccount())) throw new CommonException(EmError.USER_ADD_IS_NULL);
        //用户信息
        LambdaQueryWrapper<UserEntity> queryWrapper = new LambdaQueryWrapper<>();
        UserEntity userEntity = this.baseMapper.selectOne(queryWrapper.eq(UserEntity::getAccount, dto.getAccount()));

        if (ObjectUtil.isEmpty(userEntity)) throw new CommonException(EmError.AUTH_USER_IS_NULL);
        // 用户是否被禁用
        if (userEntity.getIsBlocked()) throw new CommonException(EmError.AUTH_USER_IS_BLOCK_LOGIN);
        // 校验密码
        if (!dto.getPassword().equals(userEntity.getPassword())) throw new CommonException(EmError.AUTH_PASSWORD_ERROR);
        //构建返回对象
        UserVo userVo = new UserVo();
        BeanUtils.copyProperties(userEntity, userVo);
        // 生成JwtToken
        long expireTime = dto.getRememberMe() ? JwtConstant.EXPIRATION_REMEMBER : JwtConstant.EXPIRATION;
        String token = JwtTokenUtil.createToken(userVo.getId().toString(), expireTime * 1000L);
        userVo.setToken(token);
        // 存入Redis，如果缓存时间更长就使用缓存时间
        Long redisExpireTime = redisUtil.getExpire(RedisPrefixConstants.PLATFORM_USER_INFO + userVo.getId());
        if (Objects.nonNull(redisExpireTime) && redisExpireTime > expireTime) {
            expireTime = redisExpireTime;
        }
        redisUtil.setObject(RedisPrefixConstants.PLATFORM_USER_INFO + userVo.getId(), userVo, expireTime);
        return userVo;
    }

    public Object resetPassword(Integer id) {
        LambdaUpdateWrapper<UserEntity> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(UserEntity::getId, id).set(UserEntity::getPassword, defaultProperties.getPassword());
        return this.update(updateWrapper);
    }


    public Boolean saveUser(UserDto dto) {
        dto.setPassword(defaultProperties.getPassword());
        return save(BeanUtil.copyProperties(dto, UserEntity.class));
    }

    public Boolean updateUser(UserDto dto) {
        return updateById(BeanUtil.copyProperties(dto,UserEntity.class));
    }

    public Boolean deleteUser(Integer id) {
        return removeById(id);
    }

    public RPage<List<UserVo>> getUserList(UserPageDto dto) {
        Page<Object> page = PageHelper.startPage(dto.getPage(), dto.getLimit());
        List<UserVo> list = this.baseMapper.getUserList(dto);
        return RPage.ok(list,dto.getPage(),dto.getLimit(),page.getTotal());
    }

    public UserVo getUserDetails(Integer id) {
        return BeanUtil.copyProperties(baseMapper.selectById(id), UserVo.class);
    }

}
